package View;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.jms.Session;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import org.hibernate.SessionFactory;
import org.hibernate.SharedSessionContract;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import Controller.ControladorCategoria;
import Model.Category;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JList;
import javax.swing.JTextArea;

public class GestionarCategoria extends JFrame {

	private JPanel contentPane;
	private JTextField textFieldNomC;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GestionarCategoria frame = new GestionarCategoria();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GestionarCategoria() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 11, 414, 240);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblNombre = new JLabel("Nombre");
		lblNombre.setBounds(58, 30, 46, 14);
		panel.add(lblNombre);
		
		JLabel lblDescripcion = new JLabel("Descripcion");
		lblDescripcion.setBounds(58, 55, 65, 14);
		panel.add(lblDescripcion);
		
		textFieldNomC = new JTextField();
		textFieldNomC.setBounds(127, 27, 204, 20);
		panel.add(textFieldNomC);
		textFieldNomC.setColumns(10);
		
		JButton button = new JButton("Add");
		button.setBounds(57, 176, 72, 23);
		panel.add(button);
		
		final JTextArea textAreaDescC = new JTextArea();
		textAreaDescC.setBounds(127, 56, 204, 59);
		panel.add(textAreaDescC);
		
		JButton btnClose = new JButton("Close");
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				GestionarCategoria.this.dispose();
			}
		});
		btnClose.setBounds(151, 176, 71, 23);
		panel.add(btnClose);

		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ControladorCategoria cont = new ControladorCategoria();
				cont.insertar(textAreaDescC.getText(), getTextFieldNomC().getText());
				GestionarCategoria.this.dispose();
			}
		});
	}

	public JTextField getTextFieldNomC() {
		return textFieldNomC;
	}

	public void setTextFieldNomC(JTextField textFieldNomC) {
		this.textFieldNomC = textFieldNomC;
	}


	
	public static void Insertar() {

		  AnnotationConfiguration config = new AnnotationConfiguration();
		  config.addAnnotatedClass(Category.class);
		  config.configure("hibernate.cfg.xml");
		  new SchemaExport(config).create(true, true);

		  SessionFactory factory = config.buildSessionFactory();
		  org.hibernate.Session session = factory.getCurrentSession();

		  session.beginTransaction();
		  Category cat = new Category();
		  GestionarCategoria gestcat = new GestionarCategoria();
		  session.save(cat);

		  session.getTransaction().commit();
		 }
}
